package com.vercon.otamirror.receiver;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.net.ConnectivityManager;

import com.abupdate.iot_libs.data.constant.BroadcastConsts;
import com.abupdate.iot_libs.engine.otaStatus.OtaStatus;
import com.abupdate.iot_libs.engine.otaStatus.OtaStatusMgr;
import com.abupdate.iot_libs.utils.NetUtils;
import com.abupdate.trace.Trace;
import com.vercon.commonmirror.utils.LogUtils;

/**
 * @author fighter_lee
 * @date 2018/7/10
 */
public class OtaReceiver extends BroadcastReceiver {

    private static final String TAG = "OtaReceiver";

    @Override
    public void onReceive(Context context, Intent intent) {
        String action = intent.getAction();
        if (null == action) {
            return;
        }
        switch (action) {
            case BroadcastConsts.ACTION_FOTA_UPDATE_RESULT:
                boolean result = intent.getBooleanExtra(BroadcastConsts.KEY_FOTA_UPDATE_RESULT, false);
                String productId = intent.getStringExtra(BroadcastConsts.KEY_FOTA_UPDATE_PRODUCTID);
                String deltaID = intent.getStringExtra(BroadcastConsts.KEY_FOTA_UPDATE_DELTAID);
                if (result) {
                    //升级成功，提示
                    showNotifyDialog();
                    LogUtils.d("升级成功");
                }
                break;
            case BroadcastConsts.ACTION_FOTA_NOTIFY:
                String msg = intent.getStringExtra(BroadcastConsts.KEY_FOTA_NOTIFY);
                Trace.i(TAG, "receiver push:" + msg);
                LogUtils.d("升级推送广播");
                break;
            case ConnectivityManager.CONNECTIVITY_ACTION:
                LogUtils.d("CONNECTIVITY_ACTION " + !NetUtils.isNetWorkAvailable());
                if (!NetUtils.isNetWorkAvailable()) {
                    return;
                }
//                Dispatcher.getDispatcher().enqueue(new NetworkChangeDispatcher());
                break;
            case Intent.ACTION_BOOT_COMPLETED:
                //判断关机前的状态
                OtaStatus curStatus = OtaStatusMgr.getInstance().getCurStatus();
                LogUtils.d("判断关机前的状态 curStatus == OtaStatus.DOWNLOAD_FINISHED " + (curStatus == OtaStatus.DOWNLOAD_FINISHED));
                if (curStatus == OtaStatus.DOWNLOAD_FINISHED) {
//                    Dispatcher.getDispatcher().enqueue(new BootCompletedDispatcher());
                }
                break;
            default:
                break;
        }
    }

    private void showNotifyDialog() {

    }
}
